package com.zhdl.network.duplexprotocol;

import com.project.command.faq.server.LinkServer;
import com.project.common.enums.PortTypeEnum;
import com.project.network.client.SerialSocketClient;
import com.project.network.conf.LinkClientSocketBuild;
import com.project.network.conf.LinkServerSocketBuild;
import com.project.network.entity.PortSocket;
import com.project.network.server.SerialSocketServer;
import com.zhdl.network.asciitcp.AsciiTcpClientHandler;
import com.zhdl.network.modbustcp.analysis.ModbusTcpListener;
import com.zhdl.network.modbustcp.dto.ModbusPacket;
import com.zhdl.network.servertcp.ModBusTcpServerHandler;
import com.zhdl.network.servertcp.ServerTcpListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.intf.Packet;
import org.tio.server.TioServerConfig;
import org.tio.utils.lock.SetWithLock;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.project.network.server.SerialSocketServer.tioServerConfig;

@Slf4j
@Component
public class DuplexProtocolBuild extends LinkServerSocketBuild {
    @Override
    public void initLinkServer() {
        List<PortSocket> portSocketList = new ArrayList<>();
        PortSocket portSocket = new PortSocket();
        portSocket.setServerIp("192.168.0.20");
//        portSocket.setServerIp("172.16.2.27");
        portSocket.setServerPort("2000");
        portSocket.setProtocols("custom_server");
        portSocket.setServerType("server");
        portSocket.setTimeouts(1);
        portSocket.setProtocolsId("1");
        portSocket.setName("custom_server");
        portSocketList.add(portSocket);
        portSocketList.forEach((p) -> {
            try {
                SerialSocketServer socket = this.bulidSerialSocketServer(p);
                if (socket != null) {
                    getLinkServerMap().put(socket.getServerIp().concat(":").concat(socket.getServerPort()), socket);
                    linkServers.add(socket);
                }
            } catch (IOException var3) {
                log.error("init socket error message [{}]", var3.getMessage());
                throw this.exceptionBuild.newException("9999", var3);
            }
        });
    }
    private SerialSocketServer bulidSerialSocketServer(PortSocket portSocket) throws IOException {
       return buildHandServer(portSocket);
    }
    @Override
    protected SerialSocketServer buildHandServer(PortSocket properties) throws IOException {
        return buildServer(properties);
    }

    private SerialSocketServer buildServer(PortSocket portSocket) throws IOException {
        String protocols = portSocket.getProtocols();
        String serverIp = portSocket.getServerIp();
        String serverPort = portSocket.getServerPort();
        CunstomTcpServerHandler handler = new CunstomTcpServerHandler(serverIp, serverPort, protocols, message);
        ServerTcpListener listener = new ServerTcpListener();
        ModbusPacket packet = new ModbusPacket();
        SerialSocketServer socket = new SerialSocketServer(handler, listener, packet, serverIp, serverPort);
        socket.openServerSocket("custom_server", serverIp, Integer.parseInt(serverPort),Integer.MAX_VALUE, handler, listener);
        duplexProtocolSendService.init(socket);
        return socket;
    }
    @Resource
    private DuplexProtocolSendService duplexProtocolSendService;
}
